<template>
    <div class="components-container">
    <!-- 存入 -->
    <sweet-modal ref="dialogIn" class="dialog" :sweetModalMaxWidth="pageWidth">
        <h3>{{title}}</h3>
        <div style="width:700px; margin:30px 50px">
            <div class="layui-form-item">
                <label class="layui-form-label">业务编号</label>
                <div class="layui-input-inline">
                    <el-input v-model="deaddata.deadseachno" placeholder="请选取逝者" @keyup.native="SeachDead($event)"></el-input>
                </div>
                <el-button type="primary" @click="openRegCheck">选取逝者</el-button>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">逝者姓名</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.deadName" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">车号</label>
                    <div class="layui-input-inline">
                        <el-input v-model="cdhdata.CARLIC" readonly></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">驾驶员</label>
                    <div class="layui-input-inline">
                        <el-input v-model="cdhdata.CDHDRIVER" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">接尸地点</label>
                    <div class="layui-input-inline">
                        <el-input v-model="cdhdata.CDHADDRESS" readonly></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">联系人</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.linkMan" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.linkTel" readonly></el-input>
                    </div>
                </div>
            </div>
            
            <div class="layui-form-item">
                <div class="layui-inline">
                <label class="layui-form-label">存入时间</label>
                <div class="layui-input-inline">
                    <el-date-picker v-date v-model="ichdataExtend.saveDates" type="datetime" class="longtext" format="yyyy-MM-dd HH:mm" placeholder="选择日期时间"> </el-date-picker>
                </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">冰柜号</label>
                    <div class="layui-input-inline">
                        <el-select v-model="icedata.iceid" filterable placeholder="请选择">
                            <el-option
                            v-for="item in icelist"
                            :key="item.ICEID"
                            :label="item.ICENAME"
                            :value="item.ICEID">
                            </el-option>
                        </el-select>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item btnCenter">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="submitAdd">提交</el-button>
        </div>
    </sweet-modal>
    <!-- 逝者选择 -->
    <sweet-modal ref="deadSelect" class="dialog">
        <h3>逝者选择</h3>
        <div class="layui-form-item">
            <label class="layui-form-label">到馆时间</label>
            <div class="layui-input-inline">
                <el-date-picker v-date v-model="regCheckDate" @change="getRegData" type="date" placeholder="选择日期时间"> </el-date-picker>
            </div>
        </div>
        <div style="height:400px;max-height:400px;overflow:auto;">
            <table class="layui-table">
                <thead>
                    <tr>
                        <th>业务编号</th>
                        <th>逝者姓名</th>
                        <th>联系人</th>
                        <th>死亡日期</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in regCheckList">
                        <td>{{item.REGBUSNUM}}</td>
                        <td>{{item.REGNAME}}</td>
                        <td>{{item.REGHANDLERNAME}}</td>
                        <td>{{item.REGDEATHDATE}}</td>
                        <td><button @click="getDead(item)" class="layui-btn layui-btn-normal">选择</button></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </sweet-modal>

    <!-- 取出 -->
    <sweet-modal ref="dialogOut" class="dialog">
        <h3>{{title}}</h3>
        <div style="width:700px; margin:30px 50px">
            <div class="layui-form-item">
                <label class="layui-form-label">业务编号</label>
                <div class="layui-input-inline">
                    <el-input v-model="deaddata.deadselectno" readonly></el-input>
                    <!-- <el-select v-model="deaddata.deadselectno" placeholder="请选择" class="longtext" @change="GetIchInfo">
                        <el-option v-for="item in IceUse" :key="item.REGID" :label="item.REGBUSNUM" :value="item.REGBUSNUM">
                        </el-option>
                    </el-select> -->
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">逝者姓名</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.deadName" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">死亡日期</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.deaddate" readonly></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">经办家属</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.linkMan" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">联系电话</label>
                    <div class="layui-input-inline">
                        <el-input v-model="deaddata.linkTel" readonly></el-input>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">存入时间</label>
                    <div class="layui-input-inline">
                        <el-date-picker type="datetime" v-model="ichdataExtend.saveDates" format="yyyy-MM-dd HH:mm" class="longtext" readonly></el-date-picker>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">取出时间</label>
                    <div class="layui-input-inline">
                        <el-date-picker v-date class="longtext" v-model="ichdataExtend.ichEndDates" type="datetime" placeholder="选择日期时间"> </el-date-picker>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item btnCenter">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="submitOut">提交</el-button>
        </div>
    </sweet-modal>

    <sweet-modal ref="dialogInfo" class="dialog" :sweetModalMaxWidth="pageWidth">
        <h3>{{title}}</h3>
        <div :style="{maxHeight:maxHeight+'px'}" style="overflow:auto;">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">冰柜名称</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.ICENAME" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">业务编号</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGBUSNUM" readonly></el-input>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">逝者姓名</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGNAME" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">性别</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGSEXs" readonly></el-input>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">死亡日期</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGDEATHDATE" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">家属姓名</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGHANDLERNAME" readonly></el-input>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">家属电话</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGHANDLERLINK" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">证件号码</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.REGCARDNO" readonly></el-input>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.CARNO" readonly></el-input>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">处理单位</label>
                    <div class="layui-input-inline">
                        <el-input v-model="ichData.PRPPROCESSINGUNIT" readonly></el-input>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">开始时间</label>
                    <div class="layui-input-inline">
                        <el-date-picker v-date type="datetime" v-model="ichDataEexend.staTime" readonly></el-date-picker>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">结束时间</label>
                    <div class="layui-input-inline">
                        <el-date-picker v-date type="datetime" v-model="ichDataEexend.endTime" readonly></el-date-picker>
                    </div>
                </div>
            </div>
               <div class="layui-form-item"  v-if="showjbr">
                          <div class="layui-inline">
                            <label class="layui-form-label">进柜人</label>
                            <div class="layui-input-inline">
                                   <el-input v-model="ichdata.ICHCHKUSR" readonly></el-input>
                            </div>
                            </div>
                          <div class="layui-inline">
                            <label class="layui-form-label">出柜人</label>
                            <div class="layui-input-inline">
                                   <el-input v-model="ichdata.ICHCABINETMAN" readonly></el-input>
                            </div>
                            </div>
                </div>

        </div>
        <div class="layui-form-item btnCenter sweet-btn">
            <el-button @click="closeDialog">取消</el-button>
        </div>
    </sweet-modal>
    </div>

</template>

<style  rel="stylesheet/scss" lang="scss" scoped>
</style>
<script>
const INFO = "详情";
import apiBase from "@/api/apiBase.js";
import sweetModal from "@/components/sweetModal";
import {
    formatDate,
    timeStringToDate,
    dateStringToDate
} from "@/utils/date.js";
export default {
    data() {
        return {
            showjbr:null,
            ichData: {
                REGSEXs: null,
            },
            ichDataEexend: {
                staTime: null,
                endTime: null
            },
            deaddata: {
                deadseachno: null,
                deadName: null,
            },
            cdhdata: {
                CARLIC: null,
                CDHADDRESS: null,
                CDHLINKMAN: null,
                CDHDRIVER: null,
            },
            ichdataExtend: {
                ichEndDates: null,
                saveDates: null,
            },
            icedata: {
                ichid: null,
                iceid: null,
                iceno: null,
                savedate: null,
                savetime: null,
                takedate: null,
                taketime: null,
                ichiceid: null,
            },
            ichdata: {
                ichid: null,
                iceid: null,
                savedate: null,
                savetime: null,
                takedate: null,
                taketime: null,
                ICHCHKUSR: null,//-- 进柜人
                ICHCABINETMAN: null,//-- 出柜人
            },
            pageWidth: 700,
            maxHeight: 500,
            title: null,
            order: null,
            icelist: [],
            regCheckDate: null,
            regCheckList: [],
            QueryDriver: [],
        };
    },
    methods: {
        open(order, id) {
            this.order = order;
            console.log(id);
            if(id != null){
                this.icedata.iceid = id.ICEID;
            }
            this.geticenotuse();
            this.dataRepeat();
            switch (this.order) {
            case INFO:
                this.title = "冰柜使用详情";
                this.dataInfo(id);
                break;
            case '存入':
                this.title = "冰柜使用开始";
                this.$refs.dialogIn.open();
                break;
            case '取出':
                this.title = "冰柜使用取出";
                // this.IceUse = ice.IceUse;
                this.deaddata.deadselectno = id.REGBUSNUM;
                this.icedata.iceid = id.ICEID;
                this.GetIchInfo();
                this.$refs.dialogOut.open();
                break;
            default:
            }
        },
        GetIchInfo() {
        let data = {
            ICHICEID: this.icedata.iceid,
            ICHREGBUSNUM: this.deaddata.deadselectno
        };
        apiBase.DoService(
            data,
            apiBase.getApiAdress().IceUse.GetRegistrationAndUseByNo,
            res => {
                this.ichInfo = res.data.content;
                let reg = res.data.content.REG;
                let ich = res.data.content.ICH;
                let cdh = res.data.content.CDH;
                if (reg) {
                    this.deaddata.deadid = reg.REGID; 
                    this.deaddata.deadName = reg.REGNAME;
                    this.deaddata.deaddate = reg.REGDEATHDATE;
                    this.deaddata.linkMan = reg.REGHANDLERNAME; 
                    this.deaddata.linkTel = reg.REGHANDLERLINK; 
                    this.deaddata.REGADDRESS = reg.REGADDRESS;
                }
                if (ich) {
                    this.ichdata.takedate = ich.ICHENDDATE;
                    this.ichdata.taketime = ich.ICHENDTIME;
                    this.ichdata.savedate = ich.ICHBEGDATE;
                    this.ichdata.savetime = ich.ICHBEGTIME;
                    this.ichdata.iceno = ich.ICENO;
                    this.ichdata.ichid = ich.ICHID;
                    this.ichdata.iceid = ich.ICHICEID;
                    this.ichdataExtend.saveDates = timeStringToDate(
                        this.ichdata.savetime, this.ichdata.savedate
                    );
                    console.log(1000);
                    console.log(this.ichdataExtend.saveDates);
                }
                if(cdh){
                    this.cdhdata.CARLIC = cdh.CARLIC;
                    this.cdhdata.CDHADDRESS = cdh.CDHADDRESS;
                    this.cdhdata.CDHLINKMAN = cdh.CDHLINKMAN;
                }
            });
        },
        //获取火化登记数据
        getRegData() {
            let data = {
                Data: {
                    RegdateEquals: formatDate(this.regCheckDate, "yyyyMMdd")
                }
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Registration.QueryRegistration,
                res => {
                    this.regCheckList = res.data.content.OutData;
                }
            );
        },
        dataRepeat() {
            this.deaddata.deadseachno = null;
            this.deaddata.deadid = null;
            this.deaddata.deadName = null;
            this.deaddata.deaddate = null;
            this.deaddata.linkMan = null;
            this.deaddata.linkTel = null;
            this.deaddata.deadRemark = null;
            this.cdhdata.CARLIC = null;
            this.cdhdata.CDHADDRESS = null;
            this.cdhdata.CDHLINKMAN = null;
            this.cdhdata.CDHADDRESS = null;
            this.cdhdata.CDHDRIVER = null;
            this.icedata.iceid = null;
            this.ichdataExtend = {};
        },
        getDead(item) {
            this.deaddata.deadseachno = item.REGBUSNUM;
            this.deaddata.deadid = item.REGID;
            this.deaddata.deadName = item.REGNAME;
            this.deaddata.deaddate = item.REGDEATHDATE;
            this.deaddata.linkMan = item.REGHANDLERNAME;
            this.deaddata.linkTel = item.REGHANDLERLINK;

            this.cdhdata.CARLIC = item.Cdh.CARLIC;
            this.cdhdata.CDHDRIVER = item.Cdh.CDHDRIVER;
            this.cdhdata.CDHADDRESS = item.Cdh.CDHADDRESS;
            this.cdhdata.CDHLINKMAN = item.Cdh.CDHLINKMAN;
            this.QueryDriver.map(item => {
                if(item.STAID === this.cdhdata.CDHDRIVER){
                    this.cdhdata.CDHDRIVER = item.STANAME; 
                }
            })
            this.$refs.deadSelect.close();
        },
        SeachDead(ev) {
            if (ev.keyCode === 13) {
                this.IsEnter = true;
                let data = {
                    ICHICEID: this.icedata.iceid,
                    ICHREGBUSNUM: this.deaddata.deadseachno
                };
                apiBase.DoService(
                    data,
                    apiBase.getApiAdress().IceUse.GetRegistrationAndUseByNo,
                    res => {
                        if (res.data.content.REG) {
                            this.deaddata.deadid = res.data.content.REG.REGID;
                            this.deaddata.deadName = res.data.content.REG.REGNAME;
                            this.deaddata.deaddate = res.data.content.REG.REGDEATHDATE;
                            this.deaddata.linkMan = res.data.content.REG.REGHANDLERNAME;
                            this.deaddata.linkTel = res.data.content.REG.REGHANDLERLINK;
                        }
                    }
                );
            } else {
                if (this.IsEnter === true) {
                    this.deaddata.deadseachno = "";
                    this.IsEnter = false;
                }
            }
        },
        geticenotuse(id) {
            apiBase.DoService({ICEID:id}, apiBase.getApiAdress().IceUse.QueryIceBynotuse, res => {
            this.icelist = res.data.content;
            });
        },
        submitAdd() {
            //确定存入
            let data = {
                //请求体
                ICHICEID: this.icedata.iceid,//冰柜id
                ICHREGBUSNUM: this.deaddata.deadseachno,//逝者编号
                ICHBEGDATE: formatDate(this.ichdataExtend.saveDates, "yyyyMMdd"),//存入时间
                ICHBEGTIME: formatDate(this.ichdataExtend.saveDates, "hhmmss") //存入日期
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().IceUse.StartUse,
                res => {
                    // this.cancel();
                    this.refreshData();
                    this.$refs.dialogIn.close();
                    this.$message.success("存入成功");
                }
            );
        },
        submitOut() {
            let data = {
                //请求体
                ICHICEID: this.icedata.iceid,
                ICHREGID: this.deaddata.deadid,
                ICHENDDATE: this.ichdata.takedate,
                ICHENDTIME: this.ichdata.taketime
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().IceUse.EndUse,
                res => {
                    this.refreshData();
                    this.$refs.dialogOut.close();
                    this.$message.success("取出成功");
                }
            );
        },
        refreshData() {
            this.$emit("refreshData");
        },
        //打开逝者选取
        openRegCheck() {
            this.regCheckDate = new Date();
            this.getRegData();
            this.$refs.deadSelect.open();
        },
        closeDialog() {
            this.$refs.dialogInfo.close();   
            this.$refs.dialogIn.close();   
        },
        sex(sex) {
            var result;
            if (sex) {
                switch (sex) {
                    case "A":
                        result = "男";
                        break;
                    case "B":
                        result = "女";
                        break;
                    case "C":
                        result = "未知的性别";
                        break;
                    case "D":
                        result = "未说明的性别";
                        break;
                    default:
                }
            } else {
                result = "D";
            }
            return result;
        },
        //登记详情
        dataInfo(id) {
            let data = {
                ICHID: id
            };

            apiBase.DoService(
                data,
                apiBase.getApiAdress().IceUse.Detail,
                res => {
                    this.ichData = res.data.content;
                    this.ichdata.ICHCHKUSR=this.ichData.ICHCHKUSR;
                    this.ichdata.ICHCABINETMAN=this.ichData.ICHCABINETMAN;
                    this.ichData.REGSEXs = this.sex(this.ichData.REGSEX);
                    this.ichDataEexend.staTime = timeStringToDate(
                        this.ichData.ICHBEGTIME,
                        this.ichData.ICHBEGDATE
                    );
                    this.ichDataEexend.endTime = timeStringToDate(
                        this.ichData.ICHENDTIME,
                        this.ichData.ICHENDDATE
                    );
                    this.$refs.dialogInfo.open();
                }
            );
        }
    },
    components: {
        sweetModal
    },
    watch: {
        "seachdataExtent.Regdate": function(val) {
            this.seachdata.RegdateEquals = formatDate(val, "yyyyMMdd");
        },
        // "ichdataExtend.saveDates": function(val) {  
        //     if (val) {
        //         this.ichdata.savedate = formatDate(val, "yyyyMMdd");
        //         this.ichdata.savetime = formatDate(val, "hhmmss");
        //     }
        // },
        "ichdataExtend.ichEndDates": function(val) {
            if (val) {
                this.ichdata.takedate = formatDate(val, "yyyyMMdd");
                this.ichdata.taketime = formatDate(val, "hhmmss");
            }
        }
    },
    created() {
        apiBase.DoService({}, apiBase.getApiAdress().Staff.QueryDriver, res => {
            this.QueryDriver = res.data.content;
        });
        //冰柜使用是否显示进柜人 出柜人
        apiBase.DoService({}, apiBase.getApiAdress().Param.GetIsShowFreezerIncomingPerson, res => {
            this.showjbr = res.data.content;
        });
    }
};
</script>

